package com.gumbou.mis.pos.service.impl;

import com.gumbou.mis.pos.entity.PosCategory;
import com.gumbou.mis.pos.entity.PosItem;
import com.gumbou.mis.pos.mapper.CategoryMapper;
import com.gumbou.mis.pos.mapper.PosItemMapper;
import com.gumbou.mis.pos.service.IPosItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (PosItem)表服务实现类
 *
 * @author makejava
 * @since 2021-10-20 18:31:28
 */
@Service("posItemService")
public class PosItemServiceImpl implements IPosItemService {
    @Resource
    private PosItemMapper posItemMapper;
    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param itemId 主键
     * @return 实例对象
     */
    @Override
    public PosItem queryById(Integer itemId) {
        return this.posItemMapper.queryById(itemId);
    }

    /**
     * 分页查询
     *
     * @param posItem 筛选条件
     * @return 查询结果
     */
    @Override
    public List<PosItem> queryAll(PosItem posItem) {
        ArrayList<PosItem> resultList = new ArrayList<>();
        List<PosItem> list = this.posItemMapper.queryAll(posItem);
        for (PosItem item : list) {
            PosCategory posCategory = this.categoryMapper.queryById(item.getCategoryId());
            item.setCategory(posCategory);
            resultList.add(item);
        }
        return resultList;
        //return posItemMapper.queryAll(posItem);
    }

    /**
     * 新增数据
     *
     * @param posItem 实例对象
     * @return 实例对象
     */
    @Override
    public PosItem insert(PosItem posItem) {
        this.posItemMapper.insert(posItem);
        return posItem;
    }

    /**
     * 修改数据
     *
     * @param posItem 实例对象
     * @return 实例对象
     */
    @Override
    public PosItem update(PosItem posItem) {
        this.posItemMapper.update(posItem);
        return this.queryById(posItem.getItemId());
    }

    /**
     * 通过主键删除数据
     *
     * @param itemId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer itemId) {
        return this.posItemMapper.deleteById(itemId) > 0;
    }

    @Override
    public long count(PosItem posItem) {
        return this.posItemMapper.count(posItem);
    }
}
